package com.brokendust.client.api.module.textfield;

import com.brokendust.client.PixelEngine;
import com.brokendust.client.api.module.GUIComponent;
import com.brokendust.client.api.module.PullPanelComponent;
import com.brokendust.client.api.module.pullpanel.PullPanel;
import com.brokendust.client.api.util.BasePosition;
import com.brokendust.client.api.util.image.ImageControl;
import com.brokendust.client.config.module.ComponentJson;
import com.brokendust.client.config.module.textfield.TextFieldJson;
import com.brokendust.client.config.module.util.ConversionJson;
import com.brokendust.client.config.module.util.InputJson;
import com.brokendust.client.manager.ClientManager;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.AbstractGui;
import net.minecraft.client.gui.IGuiEventListener;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.lwjgl.opengl.GL11;

@OnlyIn(Dist.CLIENT)
public class TextField extends AbstractGui implements IGuiEventListener, GUIComponent, PullPanelComponent {
    public int guiWidth;
    public int guiHeight;
    public int guiDWidth;
    public int guiDHeight;
    public ResourceLocation image;
    public String object_id;
    public int objectWidth;
    public int objectHeight;
    public int position;
    public int x;
    public int y;
    public boolean withinButton;
    public boolean enter;
    public boolean switchEnter;
    public StringBuffer inputContent;
    public int flashTiming;
    public int mX;
    public int mY;
    public int cursorPosition;
    public int stringPosition;

    public TextField(TextFieldJson textFieldJson, int guiWidth, int guiHeight, int guiDWidth, int guiDHeight) {
        this.inputContent = new StringBuffer();
        this.guiWidth = guiWidth;
        this.guiHeight = guiHeight;
        this.guiDWidth = guiDWidth;
        this.guiDHeight = guiDHeight;
        this.object_id = textFieldJson.getObject_name();
        this.image = ImageControl.getResourceLocation(textFieldJson.getImage());
        this.objectWidth = textFieldJson.getWidth();
        this.objectHeight = textFieldJson.getHeight();
        this.position = textFieldJson.getPosition();
        this.x = textFieldJson.getX();
        this.y = textFieldJson.getY();
    }

    @Override
    public void upData(ComponentJson componentJson) {
        if (!(componentJson instanceof TextFieldJson)) {
            return;
        }
        TextFieldJson textFieldJson = (TextFieldJson) componentJson;
        this.image = ImageControl.getResourceLocation(textFieldJson.getImage());
        this.objectWidth = textFieldJson.getWidth();
        this.objectHeight = textFieldJson.getHeight();
        this.position = textFieldJson.getPosition();
        this.x = textFieldJson.getX();
        this.y = textFieldJson.getY();
    }

    @Override
    public void renderGUI(MatrixStack matrixStack, int mouseX, int mouseY, float particleTick, int guiX, int guiY) {
        Integer[] integers = BasePosition.getObjectPoint(guiX, guiY, this.guiWidth, this.guiHeight, this.objectWidth, this.objectHeight, this.x, this.y, this.position);
        this.mX = integers[0];
        this.mY = integers[1];
        this.withinButton = (mouseX >= this.mX && mouseY >= this.mY && mouseX < this.mX + this.objectWidth && mouseY < this.mY + this.objectHeight);
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        if (this.image != null) {
            PixelEngine.minecraft.getTextureManager().bind(this.image);
            AbstractGui.blit(matrixStack, this.mX, this.mY, 0.0f, 0.0f, this.objectWidth, this.objectHeight, this.objectWidth, this.objectHeight);
        } else {
            AbstractGui.fill(matrixStack, this.mX - 1, this.mY - 1, this.mX + this.objectWidth + 1, this.mY + this.objectHeight + 1, -6250336);
            AbstractGui.fill(matrixStack, this.mX, this.mY, this.mX + this.objectWidth, this.mY + this.objectHeight, -16777216);
        }
        float iX = (float) (this.mX + 2);
        int my = this.mY;
        PixelEngine.fontRenderer.getClass();
        float iY = (float) (my + 9 / 2);
        PixelEngine.fontRenderer.drawShadow(matrixStack, this.inputContent.toString(), iX, iY, 14737632);
        if (this.enter) {
            float iiX = (float) (this.mX + 2 + PixelEngine.fontRenderer.width(this.inputContent.toString()));
            int my2 = this.mY;
            PixelEngine.fontRenderer.getClass();
            float iiY = (float) (my2 + 9 / 2);
            if (this.flashTiming < 30) {
                AbstractGui.fill(matrixStack, this.cursorPosition, (int) iiY - 1, this.cursorPosition + 1, (int) iiY + 1 + 9, -3092272);
            }
            if (this.flashTiming >= 60) {
                this.flashTiming = 0;
            }
            ++this.flashTiming;
        }
        if (this.switchEnter) {
            this.switchEnter = false;
            InputJson inputJson = new InputJson(this.object_id, this.inputContent.toString());
            ClientManager.menu.guiComponentMap.forEach((s, guiComponent) -> {
                if (guiComponent instanceof TextField) {
                    TextField textField = (TextField) guiComponent;
                    inputJson.addMessage(textField.object_id, textField.inputContent.toString());
                }
                if (guiComponent instanceof PullPanel) {
                    PullPanel pullPanel = (PullPanel) guiComponent;
                    pullPanel.pullPanelComponentMap.forEach((s1, pullPanelComponent) -> {
                        if (pullPanelComponent instanceof TextField) {
                            TextField textField2 = (TextField) pullPanelComponent;
                            inputJson.addMessage(textField2.object_id, textField2.inputContent.toString());
                        }
                    });
                }
            });
            PixelEngine.pixelConnect.send("input", ConversionJson.writeJSON(inputJson));
        }
    }

    @Override
    public void renderPull(MatrixStack matrixStack, int mouseX, int mouseY, float particleTick, int guiX, int guiY, int displayX, int displayY) {
        Integer[] integers = BasePosition.getObjectPoint(guiX, guiY, this.guiWidth, this.guiHeight, this.objectWidth, this.objectHeight, this.x, this.y, this.position);
        this.mX = integers[0];
        this.mY = integers[1];
        int mmX = this.mX + this.objectWidth;
        int mmY = this.mY + this.objectHeight;
        int ddX = displayX + this.guiDWidth;
        int ddY = displayY + this.guiDHeight;
        boolean withinRange = this.mX >= displayX - this.objectWidth && this.mX <= ddX && mmX >= displayX && mmX <= ddX + this.objectWidth;
        boolean withinRange2 = this.mY >= displayY - this.objectHeight && this.mY <= ddY && mmY >= displayY && mmY <= ddY + this.objectHeight;
        if (withinRange && withinRange2) {
            this.withinButton = (mouseX >= this.mX && mouseY >= this.mY && mouseX < this.mX + this.objectWidth && mouseY < this.mY + this.objectHeight);
            RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
            int cutLX = 0;
            if (this.mX < displayX) {
                cutLX = displayX - this.mX;
                if (cutLX > this.objectWidth) {
                    cutLX = this.objectWidth;
                }
            }
            int cutRX = 0;
            if (mmX > ddX) {
                cutRX = mmX - ddX;
            }
            int cutTY = 0;
            if (this.mY < displayY) {
                cutTY = displayY - this.mY;
            }
            int cutDY = 0;
            if (mmY > ddY) {
                cutDY = mmY - ddY;
            }
            if (this.image != null) {
                PixelEngine.minecraft.getTextureManager().bind(this.image);
                AbstractGui.blit(matrixStack, this.mX + cutLX, this.mY + cutTY, (float) cutLX, (float) cutTY, this.objectWidth - cutLX - cutRX, this.objectHeight - cutTY - cutDY, this.objectWidth, this.objectHeight);
            } else {
                AbstractGui.fill(matrixStack, this.mX - 1 + cutLX, this.mY - 1 + cutTY, this.mX + this.objectWidth + 1 - cutRX, this.mY + this.objectHeight + 1 - cutDY, -6250336);
                AbstractGui.fill(matrixStack, this.mX + cutLX, this.mY + cutTY, this.mX + this.objectWidth - cutRX, this.mY + this.objectHeight - cutDY, -16777216);
            }
            float iX = (float) (this.mX + 2);
            int my = this.mY;
            PixelEngine.fontRenderer.getClass();
            float iY = (float) (my + 9 / 2);
            GlStateManager._pushMatrix();
            double scale = Minecraft.getInstance().getWindow().getGuiScale();
            GlStateManager._blendFuncSeparate((int) (displayX * scale), (int) (displayY * scale), (int) (this.guiDWidth * scale), (int) (this.guiDHeight * scale));
            GL11.glEnable(3089);
            PixelEngine.fontRenderer.draw(matrixStack, this.inputContent.toString(), iX, iY, 14737632);
            GL11.glDisable(3089);
            GlStateManager._popMatrix();
            if (this.enter) {
                float iiX = (float) (this.mX + 2 + PixelEngine.fontRenderer.width(this.inputContent.toString()));
                int my2 = this.mY;
                PixelEngine.fontRenderer.getClass();
                float iiY = (float) (my2 + 9 / 2);
                if (this.flashTiming < 30) {
                    AbstractGui.fill(matrixStack, this.cursorPosition, (int) iiY - 1, this.cursorPosition + 1, (int) iiY + 1 + 9, -3092272);
                }
                if (this.flashTiming >= 60) {
                    this.flashTiming = 0;
                }
                ++this.flashTiming;
            }
            if (this.switchEnter) {
                this.switchEnter = false;
                InputJson inputJson = new InputJson(this.object_id, this.inputContent.toString());
                ClientManager.menu.guiComponentMap.forEach((s, guiComponent) -> {
                    if (guiComponent instanceof TextField) {
                        TextField textField = (TextField) guiComponent;
                        inputJson.addMessage(textField.object_id, textField.inputContent.toString());
                    }
                    if (guiComponent instanceof PullPanel) {
                        PullPanel pullPanel = (PullPanel) guiComponent;
                        pullPanel.pullPanelComponentMap.forEach((s1, pullPanelComponent) -> {
                            if (pullPanelComponent instanceof TextField) {
                                TextField textField2 = (TextField) pullPanelComponent;
                                PixelEngine.sendLogger(textField2.object_id + " : " + textField2.inputContent.toString());
                                inputJson.addMessage(textField2.object_id, textField2.inputContent.toString());
                            }
                        });
                    }
                });
                PixelEngine.pixelConnect.send("input", ConversionJson.writeJSON(inputJson));
            }
        }
    }

    public boolean func_231044_a_(double mouseX, double mouseY, int state) {
        if (this.withinButton) {
            this.setPosition((int) mouseX);
            return this.enter = true;
        }
        if (this.enter) {
            this.switchEnter = true;
            this.enter = false;
            return true;
        }
        return false;
    }

    public void setPosition(int mouseX) {
        int mXLength = mouseX - this.mX;
        if (this.inputContent.length() > 0) {
            int allLength = PixelEngine.fontRenderer.width(this.inputContent.toString());
            if (mXLength > allLength) {
                this.cursorPosition = this.mX + allLength;
                this.stringPosition = this.inputContent.length();
            } else {
                for (int i = 0; i < this.inputContent.length(); ++i) {
                    String beforeString = this.inputContent.substring(0, i);
                    String afterString = this.inputContent.substring(0, i + 1);
                    int beforeLength = PixelEngine.fontRenderer.width(beforeString);
                    int afterLength = PixelEngine.fontRenderer.width(afterString);
                    if (mXLength > beforeLength && mXLength < afterLength) {
                        this.cursorPosition = this.mX + beforeLength;
                        this.stringPosition = i;
                        break;
                    }
                }
            }
        } else {
            this.cursorPosition = this.mX;
            this.stringPosition = 0;
        }
    }

    public boolean func_231046_a_(int keyNumber, int p_keyPressed_2_, int p_keyPressed_3_) {
        if (this.enter) {
            if (keyNumber == 263 && this.stringPosition > 0) {
                int place = this.stringPosition - 1;
                int charLength = PixelEngine.fontRenderer.width(String.valueOf(this.inputContent.charAt(place)));
                this.cursorPosition -= charLength;
                --this.stringPosition;
            }
            if (keyNumber == 262 && this.stringPosition < this.inputContent.length()) {
                int charLength2 = PixelEngine.fontRenderer.width(String.valueOf(this.inputContent.charAt(this.stringPosition)));
                this.cursorPosition += charLength2;
                ++this.stringPosition;
            }
            if (keyNumber == 257 || keyNumber == 335) {
                this.switchEnter = true;
                this.enter = false;
            }
            if (keyNumber == 259) {
                int place = this.stringPosition - 1;
                if (place >= 0) {
                    int charLength = PixelEngine.fontRenderer.width(String.valueOf(this.inputContent.charAt(place)));
                    this.inputContent.deleteCharAt(place);
                    --this.stringPosition;
                    this.cursorPosition -= charLength;
                }
            }
            return true;
        }
        return false;
    }

    public boolean func_231042_a_(char inputChar, int p_charTyped_2_) {
        int charLength = PixelEngine.fontRenderer.width(String.valueOf(inputChar));
        if (this.cursorPosition + charLength > this.mX + this.objectWidth) {
            return false;
        }
        this.inputContent.insert(this.stringPosition, inputChar);
        ++this.stringPosition;
        this.cursorPosition += charLength;
        return true;
    }
}
